package com.edushare.common.dao;

import java.util.Date;
import java.util.List;

import org.apache.cayenne.DataObjectUtils;

import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.AcademicYearModel;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Universities;

public class CourseInstancesDAO extends CayenneBasedDAO {

	public CourseInstancesDAO(ORMInterface ormInterface) {
		super(ormInterface);
	}

	public CourseInstances getCourseInstanceById(int pk) {
		return DataObjectUtils.objectForPK(getDataContext(),
				CourseInstances.class, pk);
	}

	public boolean isActive(Courses course) {
		List<CourseInstances> ciList = course.getCourseInstances();
		for (CourseInstances c : ciList) {
			if (c.getStatus() == CourseInstances.COURSE_OPEN_LINKED_TO_BATCH
					|| c.getStatus() == CourseInstances.COURSE_OPEN_STANDALONE)
				return true;
		}
		return false;
	}

	public CourseInstances getActiveInst(Courses course) {
		List<CourseInstances> ciList = course.getCourseInstances();
		for (CourseInstances c : ciList) {
			if (c.getStatus() == CourseInstances.COURSE_OPEN_LINKED_TO_BATCH
					|| c.getStatus() == CourseInstances.COURSE_OPEN_STANDALONE)
				return c;
		}
		return null;
	}

	public void closeInst(Courses course) {
		List<CourseInstances> ciList = course.getCourseInstances();
		for (CourseInstances c : ciList) {
			if (c.getStatus() == CourseInstances.COURSE_OPEN_LINKED_TO_BATCH
					|| c.getStatus() == CourseInstances.COURSE_OPEN_STANDALONE)
				closeInst(c);
		}
	}

	public void closeInst(CourseInstances courseInst) {
		if (courseInst.getStatus() == CourseInstances.COURSE_OPEN_LINKED_TO_BATCH
				|| courseInst.getStatus() == CourseInstances.COURSE_OPEN_STANDALONE)
			courseInst.setStatus(CourseInstances.COURSE_CLOSED);
		getDataContext().commitChanges();
	}

	public void closeAdministrative(CourseInstances courseInst) {
		if (courseInst.getStatus() == CourseInstances.COURSE_OPEN_LINKED_TO_BATCH
				|| courseInst.getStatus() == CourseInstances.COURSE_OPEN_STANDALONE)
			courseInst.setStatus(CourseInstances.COURSE_CLOSED_ADMINISTRATIVE);
		getDataContext().commitChanges();
	}

	public CourseInstances newStandaloneInst(Courses course) {
		if (isActive(course))
			return null;
		CourseInstances courseInst = new CourseInstances();
		courseInst.setCourse(course);
		Departments dept = new CoursesDAO(ormInterface).getParentDept(course);
		Universities univ = new DepartmentsDAO(ormInterface).getParent(dept);
		courseInst.setInstanceYear(univ.getCurrentAcadYear());
		courseInst.setSemester(new AcademicYearModelDAO(ormInterface, univ)
				.getActiveSemester());
		courseInst.setCreationTimestamp(new Date());
		courseInst.setDescription("");
		courseInst.setStatus(CourseInstances.COURSE_OPEN_STANDALONE);
		getDataContext().registerNewObject(courseInst);
		getDataContext().commitChanges();
		return courseInst;
	}

	public CourseInstances newBatchInst(Courses course) {
		if (isActive(course))
			return null;
		CourseInstances courseInst = new CourseInstances();
		courseInst.setCourse(course);
		courseInst.setCreationTimestamp(new Date());
		courseInst.setDescription("");
		Departments dept = new CoursesDAO(ormInterface).getParentDept(course);
		Universities univ = new DepartmentsDAO(ormInterface).getParent(dept);
		courseInst.setInstanceYear(univ.getCurrentAcadYear());
		courseInst.setSemester(new AcademicYearModelDAO(ormInterface, univ)
				.getActiveSemester());
		courseInst.setStatus(CourseInstances.COURSE_OPEN_LINKED_TO_BATCH);
		getDataContext().registerNewObject(courseInst);
		getDataContext().commitChanges();
		return courseInst;
	}

	public CourseInstances getInstanceByYearAndSem(Courses course, int year,
			AcademicYearModel sem) {
		List<CourseInstances> cInstList = course.getCourseInstances();
		for (CourseInstances c : cInstList) {
			if (c.getInstanceYear() == year && c.getSemester() == sem)
				return c;
}
		return null;
	}

	public CourseInstances createInstanceByYearAndSem(Courses course, int year, 
			AcademicYearModel sem){
		CourseInstances cInst = getInstanceByYearAndSem(course, year, sem);
		if(cInst != null)
			return cInst;
		cInst = new CourseInstances();
		cInst.setCourse(course);
		cInst.setCreationTimestamp(new Date());
		cInst.setDescription("");
		cInst.setInstanceYear(year);
		cInst.setSemester(sem);
		Departments dept = new CoursesDAO(ormInterface).getParentDept(course);
		Universities univ = new DepartmentsDAO(ormInterface).getParent(dept);
		int currYear = univ.getCurrentAcadYear();
		AcademicYearModel currSem = new AcademicYearModelDAO(ormInterface, univ).getActiveSemester();
		if(currYear == year && currSem == sem)
			cInst.setStatus(CourseInstances.COURSE_OPEN_STANDALONE);
		else
			cInst.setStatus(CourseInstances.COURSE_CLOSED);
		getDataContext().registerNewObject(cInst);
		getDataContext().commitChanges();
		return cInst;
	}
}
